import { ref } from 'vue'
import router, { BASIC_ROUTES } from '@/router'
import { defineStore } from 'pinia'
import type { RouteRecordRaw } from 'vue-router'

export const appStore = defineStore('__app__', {
  state: () => {
    return {
      filterAsyncRoutes: [],
      allRoutes: ref<any>([])
    }
  },
  persist: {
    storage: localStorage,
    paths: [
      'allRoutes'
    ]
  },
  getters: {},
  actions: {
    setFilterAsyncRoutes(routes: any) {
      this.$patch((state) => {
        state.filterAsyncRoutes = routes
        state.allRoutes = BASIC_ROUTES.concat(routes)
        routes.map((item: RouteRecordRaw) => {
          router.addRoute(item)
        })
      })
    },
  }
})
